package com.acctrue.txsystem.controller;

import com.acctrue.api.RemoteUserService;
import com.acctrue.common.core.domain.LoginBody;
import com.acctrue.common.core.enums.ResultTypeEnum;
import com.acctrue.common.jwt.utils.JwtTokenUtil;
import com.acctrue.common.core.domain.R;
import com.acctrue.common.core.domain.UserToken;
import com.acctrue.common.core.domain.UserTokenInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class TestController {
    @Resource
    private RemoteUserService remoteUserService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    @GetMapping("/test")
    public R<LoginBody> test() {
        log.info("test-start");
        return remoteUserService.getUserInfo("zhangsan");
    }

    /**
     * 登录
     * @param loginBody
     * @return
     */
    @PostMapping("login")
    public R<UserToken> login(@RequestBody LoginBody loginBody){
        UserTokenInfo userTokenInfo = new UserTokenInfo();
        userTokenInfo.setUserId(1L);
        userTokenInfo.setUserName(loginBody.getUserName());
        userTokenInfo.setRealName("姜世存");
        UserToken userToken = jwtTokenUtil.createToekns(userTokenInfo);
        return R.ok(userToken);
    }

    /**
     * 刷新令牌
     * @param userToken
     * @return
     */
    @PostMapping("/refreshToken")
    public R<?> refreshToken(@RequestBody  UserToken userToken) {
        String refreshToken = userToken.getRefreshToken();
        // 判断token是否超时
        if (jwtTokenUtil.isTokenExpired(refreshToken)) {
            // 刷新令牌 放入黑名单
            jwtTokenUtil.addBlacklist(refreshToken, jwtTokenUtil.getExpirationDate(refreshToken));
            // 访问令牌 放入黑名单
            String odlAccessToken = jwtTokenUtil.getAccessTokenByRefresh(refreshToken);
            if (!StringUtils.isEmpty(odlAccessToken)) {
                jwtTokenUtil.addBlacklist(odlAccessToken, jwtTokenUtil.getExpirationDate(odlAccessToken));
            }
            return R.ok(ResultTypeEnum.TOKEN_INVALID);
        }
        // 生成新的 访问令牌 和 刷新令牌
        UserTokenInfo userInfoToken = jwtTokenUtil.getUserInfoToken(refreshToken);

        return R.ok(jwtTokenUtil.createToekns(userInfoToken));
    }

    /**
     * 登出
     * @return
     */
    @PostMapping("/logOut/{token}")
    public R logOut(@PathVariable("token") String token) {
        // 放入黑名单
        jwtTokenUtil.addBlacklist(token, jwtTokenUtil.getExpirationDate(token));
        return R.ok(ResultTypeEnum.SUCCESS);
    }

    /**
     * 注销
     * @return
     */
    @PostMapping("/logOff/{token}")
    public R logOff(@PathVariable("token") String token) {
        // 放入黑名单
        jwtTokenUtil.addBlacklist(token, jwtTokenUtil.getExpirationDate(token));
        return R.ok(ResultTypeEnum.SUCCESS);
    }

    @GetMapping("/sayHello")
    public R sayHello(@RequestParam String username){
        return R.ok(ResultTypeEnum.SUCCESS, "hello " + username);
    }

}
